#region File Description
//-----------------------------------------------------------------------------
// Game.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using kinect_music_shooter.Audio;
using kinect_music_shooter.GameWorld;
using kinect_music_shooter.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Nui.Vision; 
#endregion

namespace kinect_music_shooter
{
    /// <summary>
    /// Sample showing how to manage different game states, with transitions
    /// between menu screens, a loading screen, the game itself, and a pause
    /// menu. This main game class is extremely simple: all the interesting
    /// stuff happens in the ScreenManager component.
    /// </summary>
    public class KinectMusicShooterGame : Microsoft.Xna.Framework.Game
    {
        #region Fields
        //Tracking
        public static bool KinectIsConnected = false;
        public static bool lostUserFlag = false;

        public static NuiUserTracker _skeleton;
        public static NuiUser firstuser;
        public static Avatar avatar = new Avatar();

        //Audio
        public static MusicGraph musicGraph = new Audio.MusicGraph();

        //Sounds
        public static SoundEffect camera, error, pitch, select, killBall; 

        GraphicsDeviceManager graphics;
        ScreenManager screenManager;

        public static int width = 853;
        public static int height = 480;
        public static bool HitTheWall = false;

        // By preloading any assets used by UI rendering, we avoid framerate glitches
        // when they suddenly need to be loaded in the middle of a menu transition.
        static readonly string[] preloadAssets =
        {
            "gradient",
        };


        #endregion

        #region Initialization


        /// <summary>
        /// The main game constructor.
        /// </summary>
        public KinectMusicShooterGame()
        {
            Content.RootDirectory = "Content";

            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;
            //graphics.IsFullScreen = true;
            graphics.PreferMultiSampling = true;
            
            // Create the screen manager component.
            screenManager = new ScreenManager(this);

            Components.Add(screenManager);

            // Activate the first screens.
            screenManager.AddScreen(new BackgroundScreen(), null);
            screenManager.AddScreen(new CalibrateMenuScreen(), null);
        }


        /// <summary>
        /// Loads graphics content.
        /// </summary>
        protected override void LoadContent()
        {
            foreach (string asset in preloadAssets)
            {
                Content.Load<object>(asset);
            }

            camera = Content.Load<SoundEffect>("Sounds/camera");
            error = Content.Load<SoundEffect>("Sounds/error");
            pitch = Content.Load<SoundEffect>("Sounds/pitch");
            select = Content.Load<SoundEffect>("Sounds/select");
            killBall = Content.Load<SoundEffect>("Sounds/killball");
        }


        #endregion

        #region Draw


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);

            // The real drawing happens inside the screen manager component.
            base.Draw(gameTime);
        }


        #endregion

        #region Static Methods

        public static void saveTextureToFile(Texture2D texture, string filename)
        {
            using (System.IO.Stream stream = System.IO.File.OpenWrite(filename))
            {
                texture.SaveAsPng(stream, texture.Width, texture.Height);
            }
        }

        public static Texture2D readTextureFromFile(GraphicsDevice graphicsDevice, string filename)
        {
            try
            {
                System.IO.Stream file = TitleContainer.OpenStream(filename);
                return Texture2D.FromStream(graphicsDevice, file);
            }catch(Exception)
            {
                return null;
            }
        }

        public static Texture2D RawImageToTexture(System.Windows.Media.ImageSource Image, GraphicsDevice graphicsDevice)
        {
            System.Windows.Media.Imaging.BitmapSource bitmapsource = (System.Windows.Media.Imaging.BitmapSource)Image;
            Texture2D texture = new Texture2D(graphicsDevice, bitmapsource.PixelWidth, bitmapsource.PixelHeight, false, SurfaceFormat.Color);
            int stride = ((bitmapsource.PixelWidth * bitmapsource.Format.BitsPerPixel + 31) & (~31)) / 8;
            byte[] bytearray = new byte[stride * bitmapsource.PixelHeight];
            bitmapsource.CopyPixels(bytearray, stride, 0);
            byte[] newbytearray = new byte[640 * 480 * 4];

            int j = 0;
            for (int i = 0; i < (640 * 480 * 3); i += 3)
            {
                newbytearray[j] = bytearray[i];
                newbytearray[j + 1] = bytearray[i + 1];
                newbytearray[j + 2] = bytearray[i + 2];
                newbytearray[j + 3] = 255;
                j += 4;
            }
            texture.SetData<byte>(newbytearray);
            return texture;
        }

        #endregion

    }


    #region Entry Point

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static class Program
    {
        [STAThread]
        static void Main()
        {
            using (KinectMusicShooterGame game = new KinectMusicShooterGame())
            {
                game.Run();
            }
        }
    }

    #endregion
}
